<?php


function ShopperPressCountryTax($post){

	global $wpdb;
	$extraTax = 0;  $total = $post['price']['total'];
 
	// ADD EXTRA TAX ON FOR THE COUNTRY
 	 
		if(isset($post['billing']['country'])){
	
			$e1 = get_option("ctax_".$post['billing']['country']);
		 //die($e1."--> ".$post['price']['total']);
			if(is_numeric($e1)){
			
				$extraTax = $extraTax + ($post['price']['total']/100*$e1);
			
			}
		
		}
		
		if(isset($post['billing']['state'])){
	
			$e2 = get_option("citytax_".$post['billing']['state']);
			
			if(is_numeric($e2)){
			
				$extraTax = $extraTax + ($post['price']['total']/100*$e2);
			
			}

	}	
	
	return ShopperPressMoneyFormat($extraTax);
 
}

function ShopperPressMoneyFormat($num, $noV=""){
 
	if($num > 999){
	
		$num = number_format($num,2, '.', ',');		
		$num = str_replace(",","",$num);
 
	}else{
	
		$num = number_format($num,2, '.', ',');
	
	}
	
	if(substr($num,-3) ==".00") {
		$num = substr($num, 0, strlen($num)-3); 
	} 
	
	return $num;
 
}

	function ShopperPressCoupon($code){
	
		$ArrayCoupon = get_option("coupon_array");
	
		if(is_array($ArrayCoupon)){
			foreach($ArrayCoupon as $value){
				if($code ==$value['name']){
				
					return $value;
			
				}
			}
		}	
	}
 
	
	/*
	
	This function is used by the system is store hidden 
	
	data about the products a customer is ordering.
	
	*/
	
	function displayProducts($products) {
		
	$list = "";
		
		
		if($products != "") {

			//$products = array_reverse($products);
			foreach($products as $key => $value) {

			$list .= '<div id="ddcproduct'.$key.'"  productname="'.getProductName($key).'" qty="'.$value.'" price="'.getProductPrice($key).'" style="display:none;">
			 
			<div id="cell1">'.getProductName($key).'</div>
			<div id="cell2">'.$value.'</div>
			<div id="cell3">$'.getProductPrice($key).'</div>
			
			</div>';
				
			}

		} 
		
		return $list;
	}
	
	/* 
	
	This function is used to update the quantity.
	
	*/	

	if(!function_exists("updateQty")){ function updateQty() {
		$qty = 0;
	
		if(is_array($_SESSION['ddc']['productsincart'])){
			foreach($_SESSION['ddc']['productsincart'] as $qtyval) {
				$qty = $qty + $qtyval;
			}
		}
		$_SESSION['ddc']['cartqty'] = $qty;
		return $qty;
	} }
	
	
	
	 /*
	 
	 This function is used to get the 
	 
	 product price on the fly
	 
	 */
	
	if(!function_exists("getProductPrice")){ function getProductPrice($id, $copyID="") {

		global $wpdb;
 
		if(is_numeric($copyID) && $copyID != 0){ $id = $copyID; }

		if(function_exists('get_post_meta')){			
 
			$PriceIncludingExtra = GetPrice(get_post_meta($id, "price", true));	
						
			return $PriceIncludingExtra;	
		} 
	  }
	}
	
	function getProductName($id, $copyID="") {

		global $wpdb;

		if(is_numeric($copyID) && $copyID != 0){ $id = $copyID; }

		$post_id_7 = get_post($id); 
		$title = $post_id_7->post_title;

		return $title;
	}

	function getProductDesc($id,$copyID="") {

		global $wpdb;

		if(is_numeric($copyID) && $copyID != 0){ $id = $copyID; }

		$post_id_7 = get_post($id); 
		$title = $post_id_7->post_content;

		return $title;
	}

	function getProductImg($id, $copyID="") {

		global $wpdb;

		if(is_numeric($copyID) && $copyID != 0){ $id = $copyID; }

		return get_post_meta($id, "image", true);
	}
	

	function getCartSubtotal($products,$tax=0) {

		$total = 0;

		if($GLOBALS['shopperpress']['system'] =="credit"){
 			return $_SESSION['ddc']['price'];
		}

		if(is_array($products)){
 
			foreach($products as $key => $value) {

				if(isset($_SESSION['ddc'][$key]['main_ID'])){
					$key = $_SESSION['ddc'][$key]['main_ID'];
				}
 
				$total = $total+($value*getProductPrice($key));

			}
		}
		return $total;
	}

	function getBuyLink($data,$featured=0,$useCustomArray=0){

		global $post;

		$link=""; $fqty=1; $fcolor="na"; $fsize="na";

		if($useCustomArray ==0){
		
			if($featured ==1){
				$fID 		= $thepost->ID;
				$ftitle 	= $thepost->post_title;
				$fprice 	= GetPrice(get_post_meta($thepost->ID, "price", true));
			}else{
				$fID 		= $post->ID;
				$ftitle 	= $post->post_title;
				$fprice 	= GetPrice(get_post_meta($data->ID, "price", true));	
			}

		}else{
				$fID 		= $data['id'];
				$ftitle 	= $data['title'];
				$fprice 	= GetPrice($data['price']);
		}

	if($featured > 1){ $featured=1; }
		 
		$ftitle = preg_replace('/[^\w\d_ -]/si', '', $ftitle);

		if ($GLOBALS['shopperpress']['system'] == "cart" || $GLOBALS['shopperpress']['system'] == "amazon-store") {
	 
			$link= "addProduct('".get_option("siteurl")."/wp-content/themes/shopperpress/', '', 1, '".$fID."|ITEM|".str_replace(",","",$fprice)."|".$fsize."|".$fcolor."|".$fqty."','".$featured."'); "; 
			if(get_option("display_alertbox") =="yes"){ $link .= "CheckoutAlert('".SPEC($GLOBALS['_LANG']['_alert_add'])."', '".get_option("checkout_url")."');"; }
		}

		if (get_option("shopperpress_systeme") == "credit") {

		$link= "";
		
		}		

		return $link;	
	
	}






	function ShippingWeight(){
	
		global $wpdb;

		$TotalWeight=0;

		if(is_array($_SESSION['ddc']['productsincart'])){ 

			foreach($_SESSION['ddc']['productsincart'] as $key => $value) { 

				$lbs = get_post_meta($key, "lbs", true);	
				if($lbs == ""){
				$lbs = get_post_meta($key, "weight", true);				
				}
				if($value < 1){ $value=1; }

				if(is_numeric($lbs) && $lbs > 0){
					$TotalWeight = $TotalWeight + ($lbs * $value);
				}
			}
		
		}

	return $TotalWeight;
	
	}
	
	
	function ShopperPressShipping($total,$ExistingShippingValue="", $Weight=0){
 
		$CurrentPrice = $total;
		$total=0;

		// TABLE BASED SHIPPING	

		if(get_option("enable_priceshipping") =="1"){

			if(get_option("shipping_range1b") !="" && get_option("shipping_range1b") != 0){
	
				if($CurrentPrice >= get_option("shipping_range1a") && $CurrentPrice <= get_option("shipping_range1b")){
	
					$total += get_option("shipping_range1c");
	
				}		
			}	
	
			if(get_option("shipping_range2b") !="" && get_option("shipping_range2b") != 0){
	
				if($CurrentPrice >= get_option("shipping_range2a") && $CurrentPrice <= get_option("shipping_range2b")){
	
					$total += get_option("shipping_range2c");
	
				}		
			}	
	
			if(get_option("shipping_range3b") !="" && get_option("shipping_range3b") != 0){
	
				if($CurrentPrice >= get_option("shipping_range3a") && $CurrentPrice <= get_option("shipping_range3b")){
	
					$total += get_option("shipping_range3c");
	
				}		
			}		
	
			if(get_option("shipping_range4b") !="" && get_option("shipping_range4b") != 0){
	
				if($CurrentPrice >= get_option("shipping_range4a") && $CurrentPrice <= get_option("shipping_range4b")){
	
					$total += get_option("shipping_range4c");
	
				}		
			}

		}

		// FLAT RATE SHIPPING
		$total += get_option("shipping_cost");

		

		// WEIGHT BASED SHIPPING	
		if(get_option("enable_weightshipping") =="1"){
		
				
				if(get_option("shipping_weight1b") !="" && get_option("shipping_weight1b") != 0){
		
					if($Weight >= get_option("shipping_weight1a") && $Weight <= get_option("shipping_weight1b")){
		
						$total += get_option("shipping_weight1c");
		
					}		
				}	
		
				if(get_option("shipping_weight2b") !="" && get_option("shipping_weight2b") != 0){
		
					if($Weight >= get_option("shipping_weight2a") && $Weight <= get_option("shipping_weight2b")){
		
						$total += get_option("shipping_weight2c");
		
					}		
				}	
		
				if(get_option("shipping_weight3b") !="" && get_option("shipping_weight3b") != 0){
		
					if($Weight >= get_option("shipping_weight3a") && $Weight <= get_option("shipping_weight3b")){
		
						$total += get_option("shipping_weight3c");
		
					}		
				}		
		
				if(get_option("shipping_weight4b") !="" && get_option("shipping_weight4b") != 0){
		
					if($Weight >= get_option("shipping_weight4a") && $Weight <= get_option("shipping_weight4b")){
		
						$total += get_option("shipping_weight4c");
		
					}		
				}
		
		}
		
		
		// FREE SHIPPING DISCOUNT
		if(get_option("shipping_free") =="yes"){
 
			if( $CurrentPrice >= get_option("shipping_free_price") ){

				return 0;
	
			}		
		}
 

		return $total+$ExistingShippingValue;
	
	}


	function ShopperPressPromotions($Items, $totalPrice){

	if(!is_array($Items)) { return 0; }
	$quantity = 0; $extraDiscount =0;
	foreach($Items as $key=>$qty){
		$quantity += $qty;
	}


		// QUANTITY BASED PROMOTIONS
		if(get_option("enable_promotionqty") =="1"){		
				 
				if( $quantity >= get_option("promotion_qty1a")  && $quantity <= get_option("promotion_qty1b") ){
		 
					if(strlen(get_option("promotion_qty1c")) > 1){
						$extraDiscount += $totalPrice/100*get_option("promotion_qty1c");
					}else{
						$extraDiscount += get_option("promotion_qty1d");
					}
				}	
		
				if( $quantity >= get_option("promotion_qty2a")  && $quantity <= get_option("promotion_qty2b") ){
		 
					if(strlen(get_option("promotion_qty2c")) > 1){
						$extraDiscount += $totalPrice/100*get_option("promotion_qty2c");
					}else{
						$extraDiscount += get_option("promotion_qty2d");
					}
				}		

				if( $quantity >= get_option("promotion_qty3a")  && $quantity <= get_option("promotion_qty3b") ){
		 
					if(strlen(get_option("promotion_qty3c")) > 1){
						$extraDiscount += $totalPrice/100*get_option("promotion_qty3c");
					}else{
						$extraDiscount += get_option("promotion_qty3d");
					}
				}	
			
		}
 
		return $extraDiscount;
	
	}
	


	function GetPrice($price){


	$TotalPrice = $price+get_option("product_price_extra");

	// CURRENCY OPTIONS BUILT INTO 2.10
	
	if(!isset($_SESSION['currency_value']) || $_SESSION['currency_value']==""){
	
	$_SESSION['currency_value']=1;
	
	}

	$TotalPrice = $TotalPrice*$_SESSION['currency_value'];
	
		if(get_option("display_pricetag") == "no"){
			return "";
		}else{
			return $TotalPrice; 
		}
		
	
	}



?>